<template>
  <div>
    <el-dialog
      class="base-dialog"
      :custom-class="'panna-dialog_small'"
      title="采样单位"
      :visible.sync="chioceDialogVisible"
      :append-to-body="true"
      :before-close="handleClose"
    >
      <p slot="title">采样单位</p>
      <div>
        <el-tree
          ref="tree"
          :data="dataTree"
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
      </div>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSamplingUnitTree } from "../../api/Sampling/SamplingUnit.js";
import { formatData } from "@/utils/index";
export default {
  name: "ChoiceSamlingUnitDialog",
  props: {
    chioceDialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dataTree: [],
      samplingSample: {},
      queryForm: {
        parentLevel: 0,
        unitName: "",
      },
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      defaultProps: {
        children: "children",
        label: "unitName",
        id: "id",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    gainUnitName(node, unitName) {
      if (node !== null) {
        if (node.data !== null) {
          if (node.level !== 0 && node.level !== 1) {
            if (unitName !== null && unitName !== "") {
              unitName = node.data.unitName + "/" + unitName;
            } else {
              unitName = node.data.unitName;
            }
          }
          if (node.parent !== null) {
            return this.gainUnitName(node.parent, unitName);
          } else {
            return unitName;
          }
        }
      }
    },

    handleNodeClick(data, node) {
      var ss = this.gainUnitName(node, "");
      var content = JSON.parse(JSON.stringify(data));
      content.unitName = ss;
      this.samplingSample = content;
    },
    submit() {
      this.handleClose();
    },
    getList() {
      getSamplingUnitTree().then((res) => {
        this.dataTree = formatData(res.data, {
          value: "id",
          label: "unitName",
        });
      });
    },
    // 关闭弹框
    handleClose() {
      this.$emit("update:chioceDialogVisible", false);
      this.$emit("colseSamplingSample", this.samplingSample);
    },
  },
};
</script>
